/*
 * eeprom.c
 *
 *  Created on: Oct 27, 2016
 *      Author: duan
 */

#include "eeprom.h"

/*--------------------------------------------------------------------------------------
 *  Struct
 *-------------------------------------------------------------------------------------*/
EEPROM_IIC eeprom =
		{ .write_byte = param_write_byte, .read_byte = param_read_byte };

static u8 param_read_byte(u16 addr) {

	u32 nTempVal = 0;
	u8 nReadVal;

	nTempVal = (0x1 << 31 | 0x1 << 24 | addr << 8 | 0x0);
	rPARAM_CTRL = nTempVal;
	while (nTempVal & (0x1 << 31)) {
		nTempVal = rPARAM_CTRL;
	}

	nTempVal = rPARAM_READ;
	nReadVal = nTempVal & 0xff;
	udelay(10000);

	return nReadVal;
}

static void param_write_byte(u16 addr, u8 data) {
	u32 nTempVal = 0;

	nTempVal = (0x1 << 31 | addr << 8 | data);
	rPARAM_CTRL = nTempVal;
	while (nTempVal & (0x1 << 31)) {
		nTempVal = rPARAM_CTRL;
	}
	udelay(10000);
}

void set_param_remote(u16 addr,u8 FusionParams[64]) {
    u8 nI;

    for(nI=0;nI<64;nI++){
	    eeprom.write_byte(addr + nI, FusionParams[nI]); 
    }
}

void get_param_remote(u16 addr,u8 FusionParams[64]) {
    u8 nI;

    for(nI=0;nI<64;nI++){
	    FusionParams[nI] = eeprom.read_byte(addr + nI); 
    }
}
